## to include the current developing tkMD into tcl package search path. Wouldn't be needed for deploy.
set auto_path [concat [pwd]/../../pkg/tkMD $auto_path ]
## start of the program

package require tkMD 1.1
namespace import SimTask::*

## Set up the path parameters for SimTask
set SimTask::PdbPath [pwd]/pdb
set SimTask::TemplatePath [pwd]/template
set SimTask::JobPath [pwd]/job
set SimTask::RunPath [pwd]/run1

## Print out the parameters.
printEnv

## Set the trajectory analysis parameters. For the variables appeared in the path name, the value may not be important(they will be picked up from the path name).
# However, you still need to set up the variable here, so that the variable exists before you created your VarSpace. 
set peptides {Wimley_ARVA Wimley_ARYV Wimley_RVAV Wimley_VAVR Wimley_VAVY Wimley_VAYR Wimley_VDVY Wimley_VRAA Wimley_VVRG Wimley_YTTG}
set type { water neutral anionic}
set temp 298
set thick 26
set ionc 0.1
set anfr 0.3
set preptime 20
set eqltime 30
set orientation { o0 }
set skip 30


VarSpace options { type temp thick ionc anfr orientation preptime eqltime skip }

puts "Has [check_remainning_workpower] CPU left"

## Do the analysis
ana $SimTask::RunPath eql.dcd traj.inp options [pwd]/run2/@peptide/type_@type/run@i/ --combinejob orientation i --skip { ![file exists eql_min.crd] || [file exists eql.dat] } --post { grep 'alpha' traj.out > 2nd.dat.temp }

## Wait for all analysis job to be finished.
waitfor ana
## Clean up.
Path::rmfiles $SimTask::RunPath  traj.out


## A example of post processing on 2nd.dat.temp to get secondary structure information.

proc getHelicity { input output } {
    set ifile [open $input r]
    set ofile [open $output w]
    set i 1
    puts $ofile "## I alpha% beta%"
    foreach line [split [read $ifile] \n] {
        set result [regexp -all -inline -- {\(\s+(\d+)\%\)} $line ]
        if {[llength $result] != 4} {continue }
        set alpha [lindex $result 1]
        set beta [lindex $result 3]
        puts $ofile "$i $alpha $beta"
        incr i
    }
    close $ofile
    close $ifile
}

## Parse each file.
foreach secfile [Path::searchfiles $SimTask::RunPath 2nd.dat.temp] {
    set dir [file dirname $secfile]
    getHelicity $secfile $dir/2nd.dat
    exec rm $secfile
}

